home *** CD-ROM | disk | FTP | other *** search
/ Just Call Me Internet / Just Call Me Internet.iso / prog / atari / c / nos042_s / fileops.c < prev    next >
C/C++ Source or Header  |  1994-09-16  |  3KB  |  114 lines

  1. /*
  2.     fileops - extended form of file operation functions which
  3.                  convert any '/' seperators in the filename to '\' then call
  4.               the original library functions.
  5.  
  6.                  statx is not mapped in global.h
  7. */
  8.  
  9. #include <stdio.h>
  10. #include <fcntl.h>
  11. #include <sys/types.h>
  12. #include <sys/stat.h>
  13. #include <unistd.h>
  14.  
  15. static void path2dos(const char *path, char *newpath)
  16. {
  17.     const char *p     = path;
  18.     char *q             = newpath;
  19.     int  sep         = 0;
  20.  
  21.     for ( ; *p; p++) {                             /* copy string                            */
  22.         if ((*q = *p) == '/')                    /* translate seperator                */
  23.             *q = '\\';                            
  24.         if (*q == '\\') {                            /* suppress repeated seperators    */
  25.             if (!sep)
  26.                 q++;
  27.             sep = 1;
  28.         }
  29.         else {
  30.             sep =0;
  31.             q++;
  32.         }
  33.     }
  34.             
  35.     *q = '\0';                                        /* string terminator                    */
  36.  
  37.     return;
  38. }
  39.     
  40.  
  41. FILE *fopenx(const char *name, const char *mode)
  42. {
  43.     char namex[FILENAME_MAX];                    /* buffer for translated str      */
  44.  
  45.     path2dos(name, namex);                        /* convert path                        */
  46.     return fopen(namex, mode);                    /* call fopen function                */
  47. }
  48.  
  49. int    removex(const char *name)
  50. {
  51.     char namex[FILENAME_MAX];                    /* buffer for translated str      */
  52.  
  53.     path2dos(name, namex);                        /* convert path                        */
  54.     return remove(namex);                        /* call remove function                */
  55. }
  56.  
  57. int    renamex(const char *old, const char *new)
  58. {
  59.     char oldx[FILENAME_MAX];                /* buffer for translated str      */
  60.     char newx[FILENAME_MAX];                /* buffer for translated str      */
  61.  
  62.     path2dos(old, oldx);                        /* convert path                        */
  63.     path2dos(new, newx);
  64.     return rename(oldx, newx);                /* call remove function                */
  65. }
  66.  
  67. int    openx(const char *name, int mode, int prot)
  68. {
  69.     char namex[FILENAME_MAX];                    /* buffer for translated str      */
  70.  
  71.     path2dos(name, namex);                        /* convert path                        */
  72.     return open(namex, mode, prot);            /* call open function                */
  73. }
  74.  
  75. int    creatx(const char *name, int prot)
  76. {
  77.     char namex[FILENAME_MAX];                    /* buffer for translated str      */
  78.  
  79.     path2dos(name, namex);                        /* convert path                        */
  80.     return creat(namex, prot);                    /* call creat function                */
  81. }
  82.  
  83. int    unlinkx(const char *name)
  84. {
  85.     char namex[FILENAME_MAX];                    /* buffer for translated str      */
  86.  
  87.     path2dos(name, namex);                        /* convert path                        */
  88.     return unlink(namex);                        /* call unlink function                */
  89. }
  90.  
  91. int    accessx(const char *name, int mode)
  92. {
  93.     char namex[FILENAME_MAX];                    /* buffer for translated str      */
  94.  
  95.     path2dos(name, namex);                        /* convert path                        */
  96.     return access(namex, mode);                /* call remove function                */
  97. }
  98.     
  99. int    mkdirx(const char *path)
  100. {
  101.     char pathx[FILENAME_MAX];                    /* buffer for translated str        */
  102.  
  103.     path2dos(path, pathx);                        /* convert path                        */
  104.     return mkdir(pathx, 0);                        /* call mkdir function                */
  105. }
  106.  
  107. int    statx(const char *path, struct stat *statbuf)
  108. {
  109.     char pathx[FILENAME_MAX];
  110.  
  111.     path2dos(path, pathx);
  112.     return stat(pathx, statbuf);
  113. }
  114.